/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vue;

import controller.AbstractController;
import controller.PartieController;
import controller.Vue;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Observable;
import javax.swing.JOptionPane;
import model.gestion_jeu.Partie;
import org.jdesktop.application.SingleFrameApplication;

/**
 *
 * @author NABIL
 */
public final class DamierView extends AbstractDamierView implements Vue{
    
    private static DamierView instance = null;
    public static DamierView getInstance()
    {
        return instance;
    }
    
    public DamierView(SingleFrameApplication app)
    {
        super(app);
        DamierView.instance = this;
         monController = new PartieController(this);
        mainPanel.add(this.depart, BorderLayout.CENTER);
        depart.setLayout(new GridBagLayout());
        GridBagConstraints c= new GridBagConstraints();
        c.gridheight = 1;
        c.insets = new Insets(10,10,10,10);
        c.gridwidth = 1;
        c.gridx = c.gridy = 0;
        depart.add(jPanel1,c);
        c= new GridBagConstraints();
        c.gridheight = 1;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(10,10,10,10);
        depart.add(jPanel2,c);
        c.gridheight = 1;
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(10,10,10,10);
        depart.add(demarrer,c);
       mainPanel.add(new Demarrer(), BorderLayout.CENTER);
    }
    
   public AbstractController getControl()
   {
       return this.monController;
   }
    
   public void creerPartie(String blanc,String noir,Partie p)
   {
       this.partie = new PartieVue(blanc, noir,p);
       this.mainPanel.removeAll();
       this.mainPanel.add(this.partie);
       this.mainPanel.updateUI();
   }
   public void setPanelDemarrage(String nom1, String nom2, String erreur1, String erreur2)
   {
       this.mainPanel.removeAll();
       this.mainPanel.add(new Demarrer(nom1,nom2,erreur1,erreur2));
       this.mainPanel.updateUI();
   }
   
   public void displayTour()
   {
       JOptionPane.showMessageDialog(this.getFrame(), "<html>C'est à <b>"+this.partie.getNomJoueurTour()+"</b> de jouer.</html>",
               "Changement de tour",JOptionPane.INFORMATION_MESSAGE);
   }
    public void update(Observable o, Object arg) {
    try
    {
         Partie damierModel = (Partie)o;
 
         if(damierModel.isFinie())
         {
             String n =damierModel.getVainqueur().getNom();
             int i = JOptionPane.showConfirmDialog(this.getFrame(), "<html><b>"+n+"</b> a gagné la partie! Voulez vous rejouer?</html>",
                     "La partie est finie",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
             if(i==JOptionPane.YES_OPTION)
                 this.monController.initialisation(damierModel.getJoueurBlanc().getNom(), damierModel.getJoueurNoir().getNom());
             else
                 setPanelDemarrage("","","","");
         }
            this.partie.setTour(damierModel.getCouleurTour());
         this.mainPanel.updateUI();
    }catch(Exception e)
    {
        e.printStackTrace();
        System.out.println(e);
    }
}
}
